<template>
  <div class="app-container">


    <el-tree style=" display: inline-block"
      :data="data"
      :props="props"
      node-key="id"
      default-expand-all
      :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span style="margin-left: 50px">
          <el-button  type="text" :style="{'visibility': node.level === 3 ? 'hidden' : 'visible'}" size="mini" @click="() => append(node,data)">增加</el-button>
           <el-button :style="{'visibility': node.level === 1 ? 'hidden' : 'visible'}" type="text" size="mini" @click="() => update(node,data)">修改</el-button>
          <el-button  :style="{'visibility': node.level === 1 ? 'hidden' : 'visible'}" type="text" size="mini" @click="() => remove(node, data)">删除</el-button>
        </span>
      </span>
    </el-tree>


    <!-- 添加或修改常见质量通病目录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="目录名称" prop="catalogueName">
          <el-input v-model="form.catalogueName" placeholder="请输入目录名称" />
        </el-form-item>
        <el-form-item label="上级目录" prop="cataloguePid">
          <el-input v-model="form.cataloguePid" disabled />
        </el-form-item>
        <el-form-item label="排序" prop="catalogueSort">
          <el-input v-model="form.catalogueSort" type="number" placeholder="请输入排序(只能输入数字)" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="form.level" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKnowledgeCatalogue,
  getKnowledgeCatalogue, delKnowledgeCatalogue,
  addKnowledgeCatalogue, updateKnowledgeCatalogue,getCatalogueTree } from "@/api/knowledge/knowledgeCatalogue";

export default {
  name: "KnowledgeCatalogue",
  data() {
    return {
      //树的数据
      data:[

      ],
      props:{
        //没有label属性,所以将label属性改为catalogueName
        label: 'catalogueName',
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 常见质量通病目录表格数据
      knowledgeCatalogueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getTree();
  },
  methods: {
    //获取目录树
    getTree(){
      getCatalogueTree()
        .then((response)=>{
          //响应数据只有根节点,但是我需要[根节点]
            this.data = [response.data];
          }
        )
        .catch(()=>{})
    },
    append(node,data){
      this.title="添加";
      this.open = true;
      this.reset();
      //上级回显
      this.form.cataloguePid = data.catalogueId
      this.form.level = data.level+1
    },
    update(node,data){
      this.title="修改";
      this.open = true;
      this.reset();
      getKnowledgeCatalogue(data.catalogueId)
        .then((response)=>{
          this.form = response.data
          this.form.catalogueName = node.level
        }).catch(()=>{})
    },
    remove(node,data){
      this.$modal.confirm('确定要删除吗？')
        .then(()=>{
         return   delKnowledgeCatalogue(data.catalogueId)
            .then(response => {
              this.$modal.msgSuccess("删除成功");
              this.getTree();
            })
        }).catch(()=>{})

    },
    /** 查询常见质量通病目录列表 */
    getList() {
      this.loading = true;
      listKnowledgeCatalogue(this.queryParams).then(response => {
        this.knowledgeCatalogueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        catalogueId: null,
        catalogueName: null,
        cataloguePid: null,
        catalogueSort: null,
        level: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.catalogueId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加常见质量通病目录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const catalogueId = row.catalogueId || this.ids
      getKnowledgeCatalogue(catalogueId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改常见质量通病目录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.catalogueId != null) {
            updateKnowledgeCatalogue(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getTree();
            });
          } else {
            addKnowledgeCatalogue(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getTree();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const catalogueIds = row.catalogueId || this.ids;
      this.$modal.confirm('是否确认删除常见质量通病目录编号为"' + catalogueIds + '"的数据项？').then(function() {
        return delKnowledgeCatalogue(catalogueIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('knowledge/knowledgeCatalogue/export', {
        ...this.queryParams
      }, `knowledgeCatalogue_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
